#include <qopengl.h>
#include <QImage>
#include <QtOpenGL/QGLWidget>
#include "gltexture.h"

GLTexture2D::GLTexture2D()
{
}

GLTexture2D::~GLTexture2D(){
    release();
}

bool GLTexture2D::create(){
    glGenTextures(1,(GLuint*)&_tex_id);

    if(glGetError()==GL_NO_ERROR) return true;
    else return false;
}

void GLTexture2D::bind(){
    glBindTexture(GL_TEXTURE_2D, _tex_id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}

void GLTexture2D::release(){
    glDeleteTextures(1,(GLuint*)&_tex_id);
}

bool GLTexture2D::load(QString fname){
    QImage _img = QImage(fname);
    QImage _cvt;
    if(_img.isNull()) return false;
    _cvt = QGLWidget::convertToGLFormat(_img);
    return load((char*)_cvt.bits(), _cvt.width(), _cvt.height());
}

bool GLTexture2D::load(char *data, int _w, int _h ){
    bind();
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,_w,_h,0,GL_RGBA,GL_UNSIGNED_BYTE,data);
    if(glGetError()==GL_NO_ERROR) return true;
    else return false;
}


